<?php
namespace App\Controller;
use Think\Controller;
class ActiveController extends Controller {
    // 查看摇一摇活动状态
    public function getsharkflag(){
        $openid=$_POST['openid'];

        if($openid){

            $user=M('user');
            $user_select_res=$user->where(array('user_openid'=>$openid))->select();
            if($user_select_res){
                $user_id=$user_select_res[0]['id'];        
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'您不能参与此活动'));
                return ;        
            }


            $active=M('active');
            
            $select_data=array(
                'activity_type'=>1,
            );
    
            $active_select_res=$active->where($select_data)->order('id desc')->select();
            
            if($active_select_res){
                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'获取成功','return_body'=>$active_select_res[0]));
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'没有获取到数据'));
            }


        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'登录超时'));
        }

    }

    // 储存用户摇一摇次数
    public function savesharknum(){
        $openid=$_POST['openid'];
        $shake_num=$_POST['shakeNum'];
        $active_id=$_POST['activeId'];


        if(!$active_id || !$openid || (!$shake_num && $shake_num!=0)){
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));
            return ;
        }

        // 查询活动是否已经结束
        $active=M('active');
        $select_data=array(
            'activity_type'=>1,
        );
        $active_select_res=$active->where($select_data)->order('id desc')->select();

        $active_flag=$active_select_res[0]['activity_flag'];

        if($active_flag==0){
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'活动已结束'));
            return ;
        }




        $user=M('user');
        $user_select_res=$user->where(array('user_openid'=>$openid))->select();
        if($user_select_res){
            $user_id=$user_select_res[0]['id'];        
        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'没有该用户'));
            return ;        
        }

        $shark_num=M('shark_num');

        $select_data=array(
            'user_id'=>$user_id,
            'user_openid'=>$openid,
            'active_id'=>$active_id
        );

        $shark_num_select_res=$shark_num->where($select_data)->select();

        if($shark_num_select_res){
            $select_shark_num=$shark_num_select_res[0]['shark_num'];

            if($select_shark_num>$shake_num){
                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'修改成功','return_body'=>$shark_num_select_res[0]));
            }else{
                $update_data=array(
                    'user_id'=>$user_id,
                    'user_openid'=>$openid,
                    'shark_num'=>$shake_num,
                    'active_id'=>$active_id,
                    'unique_id'=>build_order_no()
                );
                $shark_num_update_res=$shark_num->where($select_data)->save($update_data);
                if($shark_num_update_res){
                    $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'修改成功','return_body'=>$update_data));
                }else{
                    $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'修改失败'));
                }
            }
        }else{
            $add_data=array(
                'user_id'=>$user_id,
                'user_openid'=>$openid,
                'shark_num'=>$shake_num,
                'active_id'=>$active_id,
                'unique_id'=>build_order_no()
            );
            $shark_num_add_res=$shark_num->data($add_data)->add();

            if($shark_num_add_res){
                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'添加成功','return_body'=>$add_data));
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'添加失败'));
            }


        }

    }


    // 通过openid 获取用户中奖列表
    public function getdrawbyopenid(){
        $openid=$_POST['openid'];
        if($openid){
            $winning_record=D('WinningRecord');

            $select_data=array(
                'user_openid'=>$openid
            );

            $winning_record_select_res=$winning_record->relation(true)->where($select_data)->order('winning_type,create_time desc')->select();

            if($winning_record_select_res){
                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'获取成功','return_body'=>$winning_record_select_res));
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'暂未中奖'));    
            }
        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));
        }
    }


    // 检查用户是否已经签到
    public function checklogin(){
        $openid=$_POST['openid'];

        if($openid){
            $user=M('user');
            $user_select_res=$user->where(array('user_openid'=>$openid))->select();
            if($user_select_res){
                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'已签到'));
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'未签到'));
            }
        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));
        }
        

    }

    // public function getdrawbyopenid(){
    //     $openid=$_POST['openid'];
    //     if($openid){
    //         $draw_user=D('draw_user');
    //         $select_data=array(
    //             'user_openid'=>$openid
    //         );

    //         $draw_user_select_res=$draw_user->relation(true)->where($select_data)->select();

    //         if($draw_user_select_res){
    //             $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'获取成功','return_body'=>$draw_user_select_res));
    //         }else{
    //             $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'获取失败'));    
    //         }
    //     }else{
    //         $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));
    //     }
    // }
}
